<?php

/**
 * @author Tuan Anh
 * @copyright 2012
 */
    
class memberController extends baseController{
    public function index(){
        if(!isset($_POST['submit'])){
            $this->master($this->registry->template->member($_SESSION['uid'])) ;
        }else{
            $_POST['uid'] = $_SESSION['uid'];
            if($this->registry->db->getManager('userManager')->updateinfo($_POST)){
                $this->master($this->registry->template->masterbody("Cập nhật thành công!"));
            }else{
                $this->master($this->registry->template->masterbody("Cập nhật thất bại!"));
            }
        }

    }

    public function doimatkhau(){
        if(!isset($_POST['submit'])){
            $this->master($this->registry->template->doimatkhau()) ;
        }else{
            if($_POST['oldpassword'] == '' | $_POST['newpassword'] == '' | $_POST['confirm'] == ''){
                $this->master($this->registry->template->masterbody("Vui lòng nhập tất cả thông tin!"));
            }
            else if($_POST['newpassword'] == $_POST['confirm']){
                $_POST['uid'] = $_SESSION['uid'];
                    if($this->registry->db->getManager('userManager')->changepassword($_POST)){
                        $this->master($this->registry->template->masterbody("Cập nhật thành công!"));
                    }else{
                        $this->master($this->registry->template->masterbody("Mật khẩu cũ không đúng!"));
                    }
            }else{
                $this->master($this->registry->template->masterbody("Mật khẩu mới không trùng khớp!"));
            }

        }

    }

    public function tindadang(){
        $this->master($this->registry->template->tindadang($_SESSION['uid'])) ;
    }
    
    public function logout(){
        $expire = 60*60*24*365;
        setcookie("username","a",time()-$expire, "/");
        setcookie("hash", "b", time()-$expire, "/");
        session_destroy();
        header("Location:".__SERVER);
    }
    
    public function changepass(){
        if(isset($_POST['newpass'])){//already post
            if($_POST['oldpass'] == "" or $_POST['newpass'] == "" or $_POST['renewpass'] == "" ){//not enough info
                //TO-DO
            }else if($_POST['newpass'] != $_POST['renewpass']){
                //TO-DO
            }else{
                $this->registry->db->getManager("userManager")->changePassword($_SESSION['uid'], $_POST['oldpass'], $_POST['newpass']);
                header("Location:".__SERVER);
            }
        }else{//not post yet
            $tags = array();
            echo $this->registry->template->getTemplate("changepass", $tags);
        }
    }
    
    public function personal(){
        $tags = array(
            "menuPrivate"   => $this->registry->template->getTemplate("menuPrivate", array()),
            "infoPrivate"   => $this->registry->template->getTemplate("infoPrivate", array()),
        );
        
        $_SESSION['tags_index']['loctinModule'] ="";
        $_SESSION['tags_index']['functionModule'] ="";
        $_SESSION['tags_index']['menuModule'] = $this->registry->template->getTemplate("menuPrivate", array());
        $_SESSION['tags_index']['contentModule'] = $this->registry->template->getTemplate("trangcanhan", array());
        echo $this->registry->template->getTemplate("index", $_SESSION['tags_index']);
    }
    
    
    
    
}

?>